<template>
  <div>
    <div class="login-container">
      <!-- 登录表单 -->
      <div class="login-body">
        <el-tooltip class="item" effect="dark" content="回到首页" placement="top">
          <button class="back-btn" @click="backToHome">
            <i class="el-icon-back"></i>
          </button>
        </el-tooltip>


        <!-- 登录表单 -->
        <div v-show="currentForm === 'login'" class="form-container">
          <div class="form-header">
            <h2 class="form-title">账号密码登录</h2>
            <p class="form-subtitle">欢迎回来,请输入您的账号</p>
          </div>

          <el-form :model="loginForm" :rules="rules" ref="ruleFrom">
            <el-form-item class="form-item" prop="username">
              <el-input prefix-icon="el-icon-user-solid" v-model="loginForm.username" placeholder="  请输入邮箱"
                @keyup.enter.native="handleLogin" size="large" />
            </el-form-item>

            <el-form-item class="form-item" prop="password">
              <el-input prefix-icon="el-icon-lock" v-model="loginForm.password" placeholder="  请输入密码"
                @keyup.enter.native="handleLogin" show-password size="large" />
            </el-form-item>

            <div class="form-options">
              <el-checkbox v-model="rememberMe">记住我</el-checkbox>
            </div>

            <el-form-item class="form-item">
              <el-button class="submit-btn ripple" :loading="loading" @click="handleLogin" type="primary">
                登 录
              </el-button>
            </el-form-item>
          </el-form>

          <div class="form-switch">
            <a @click="switchForm('register')">立即注册</a>
            <span class="divider-line">|</span>
            <a @click="switchForm('forgot')">忘记密码?</a>
          </div>

          <!-- 第三方登录 -->
          <div class="divider">
            <el-divider>选择登录方式</el-divider>
          </div>

          <div class="third-party-login">
            <div v-for="item in loginTypes" @click="handleThirdPartyLogin(item.type)">
              <el-tooltip :content="item.title" placement="top">
                <div class="login-icon">
                  <svg-icon :icon-class="item.icon"></svg-icon>
                </div>

              </el-tooltip>
            </div>
          </div>
        </div>

        <!-- 注册表单 -->
        <div v-show="currentForm === 'register'" class="form-container">
          <div class="form-header">
            <h2 class="form-title">注册账号</h2>
            <p class="form-subtitle">欢迎注册,请输入您的账号</p>
          </div>
          <el-form :model="registerForm" :rules="rules" ref="registerForm">
            <el-form-item  class="form-item" prop="nickname">
              <el-input prefix-icon="el-icon-user" v-model="registerForm.nickname" placeholder="  请输入昵称" />
            </el-form-item>

            <el-form-item class="form-item" prop="email">
              <el-input prefix-icon="el-icon-message" v-model="registerForm.email" placeholder="  请输入邮箱" />
            </el-form-item>

            <el-form-item class="form-item" prop="code">
              <el-input prefix-icon="el-icon-key" v-model="registerForm.code" placeholder="  请输入验证码">
                <template slot="append">
                  <el-button @click="sendRegisterCode" :disabled="codeSending">
                    {{ codeButtonText }}
                  </el-button>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item class="form-item" prop="password">
              <el-input prefix-icon="el-icon-lock" v-model="registerForm.password" placeholder="  请输入密码" show-password />
            </el-form-item>

            <el-form-item class="form-item">
              <el-button class="submit-btn" :loading="loading" @click="handleRegister">
                注 册
              </el-button>
            </el-form-item>

            <div class="form-switch">
              已有账号？<a @click="switchForm('login')">立即登录</a>
            </div>
          </el-form>
        </div>

        <!-- 忘记密码表单 -->
        <div v-show="currentForm === 'forgot'" class="form-container">
          <div class="form-header">
            <h2 class="form-title">找回账号</h2>
            <p class="form-subtitle">重置密码,请输入您的邮箱</p>
          </div>
          <el-form :model="forgotForm" :rules="rules" ref="forgotForm">
            <el-form-item class="form-item" prop="email">
              <el-input prefix-icon="el-icon-message" v-model="forgotForm.email" placeholder="  请输入注册邮箱" />
            </el-form-item>

            <el-form-item class="form-item" prop="code">
              <el-input prefix-icon="el-icon-key" v-model="forgotForm.code" placeholder="  请输入验证码">
                <template slot="append">
                  <el-button @click="sendVerificationCode" :disabled="codeSending">
                    {{ codeButtonText }}
                  </el-button>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item class="form-item" prop="password">
              <el-input prefix-icon="el-icon-lock" v-model="forgotForm.password" placeholder="  请输入新密码" show-password />
            </el-form-item>

            <el-form-item class="form-item">
              <el-button class="submit-btn" :loading="loading" @click="handleResetPassword">
                重置密码
              </el-button>
            </el-form-item>

            <div class="form-switch">
              <a @click="switchForm('login')">返回登录</a>
            </div>
          </el-form>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { getWebConfigApi, reportApi, getNoticeApi } from '@/api/site'
import { disableScroll, enableScroll } from "@/utils/scroll";
import {
  sendEmailCodeApi,
  registerApi,
  forgotPasswordApi,
  getJuHeLoginApi,
  getCaptchaSwitchApi,
  getJuHeIsLoginApi,
} from "@/api/auth";
import { setCookie } from "@/utils/cookie";
import SliderVerify from "./components/SliderVerify.vue";
export default {
  name: "Login",
  components: {
    SliderVerify,
  },
  data() {
    return {
      rememberMe: false,
      currentForm: "login",
      cxid: "",
      loading: false,
      wechatForm: {
        code: "",
        showQrcode: false,
      },
      countdown: 0,
      loginForm: {
        username: "",
        password: "",
        source: "PC",
      },
      registerForm: {
        nickname: "",
        email: "",
        password: "",
        code: "",
      },
      forgotForm: {
        email: "",
        code: "",
        password: "",
      },
      loginTypes: {
        qq: {
          title: "QQ账号登录",
          icon: "qq",
          type: 1,
        },
        wx: {
          title: "微信扫码登录",
          icon: "weichat",
          type: 2,
        },
        alipay: {
          title: "支付宝扫码登录",
          icon: "alipay",
          type: 3,
        },
        sina: {
          title: "微博账号登录",
          icon: "weibo",
          type: 4,
        },
        baidu: {
          title: "百度账号登录",
          icon: "baidu",
          type: 5,
        },
        huawei: {
          title: "华为账号登录",
          icon: "huawei",
          type: 6,
        },
        xiaomi: {
          title: "小米账号登录",
          icon: "xiaomi",
          type: 7,
        },
        microsoft: {
          title: "微软账号登录",
          icon: "weiruan",
          type: 8,
        },
        dingding: {
          title: "钉钉账号登录",
          icon: "dingding",
          type: 9,
        },
        gitee: {
          title: "Gitee账号登录",
          icon: "gitee",
          type: 10,
        },
        github: {
          title: "GitHub账号登录",
          icon: "github",
          type: 11,
        },

        douyin: {
          title: "抖音账号登录",
          icon: "douyin",
          type: 12,
        },

      },
      codeSending: false,
      codeButtonText: "发送验证码",
      codeTimer: null,
      pollingTimer: null,
      isShowSliderVerify: false,
      sliderVerify: null,
      showIframeDialog: false,
      iframeSrc: '',
      iframeTitle: '',
      rules: {
        nickname: [
          { required: true, message: "请输入昵称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 50,
            message: "长度在 3 到 50 个字符",
            trigger: "blur",
          },
        ],
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { type: "email", message: "请输入正确的邮箱", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      },

    };
  },

  created() {
    Object.keys(this.loginTypes).forEach((key) => {
      if (!this.$store.state.webSiteInfo.loginTypeList.includes(key)) {
        delete this.loginTypes[key];
      }
    });



  },
  methods: {
    async login() {
      this.loading = true;
      try {
        await this.$store.dispatch("loginAction", this.loginForm);
        this.$refs.sliderVerify?.verifySuccessEvent();
        this.$message.success("登录成功");
        this.handleClose();
      } catch (error) {
        this.$message.error(error.message || "登录失败，请重试");
      } finally {
        this.loading = false;
      }
    },

    /**
     * 切换表单
     * @param form
     */
    switchForm(form) {
      this.currentForm = form;
      this.loading = false;
    },
    /**
     * 登录
     */
    async handleLogin() {
      this.$refs["ruleFrom"].validate(async (valid) => {
        if (valid) {
          getCaptchaSwitchApi().then((res) => {
            if (!res.data || res.data.configValue === "Y") {
              this.isShowSliderVerify = true;
            } else {
              this.login();
            }
          });
        } else {
          return false;
        }
      });
    },
    /**
     * 注册
     */
    async handleRegister() {
      this.$refs["registerForm"].validate(async (valid) => {
        if (valid) {
          this.loading = true;
          try {
            await registerApi(this.registerForm);
            this.$message.success("注册成功");
            this.switchForm("login");
          } catch (error) {
            this.$message.error(error.message || "注册失败，请重试");
          } finally {
            this.loading = false;
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    /**
     * 忘记密码
     */
    async handleResetPassword() {
      this.$refs["forgotForm"].validate(async (valid) => {
        if (valid) {
          this.loading = true;
          try {
            // 调用重置密码接口
            await forgotPasswordApi(this.forgotForm);
            this.$message.success("密码重置成功");
            this.switchForm("login");
          } catch (error) {
            this.$message.error(error.message || "重置失败，请重试");
          } finally {
            this.loading = false;
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    /**
     * 发送忘记密码邮箱验证码
     */
    async sendVerificationCode() {
      if (this.codeSending) return;

      if (!this.forgotForm.email) {
        this.$message.error("请先输入邮箱");
        return;
      }

      this.codeSending = true;
      this.sendEmailCode(this.forgotForm.email);
    },

    /**
     * 第三方登录
     */
    handleThirdPartyLogin(type) {
      getJuHeLoginApi(type).then((res) => {
        if (res.code === 200) {
          window.open(res.logurl, "_self");
        }
      });
    },

    /**
     * 关闭登录弹窗
     */
    handleClose() {
      this.$router.go(-1);
    },

    /**
     * 发送邮箱验证码
     */
    sendRegisterCode() {
      if (this.codeSending) return;

      if (!this.registerForm.email) {
        this.$message.error("请先输入邮箱");
        return;
      }
      this.codeSending = true;
      this.sendEmailCode(this.registerForm.email);
    },

    /**
     * 发送邮箱验证码
     */
    sendEmailCode(email) {
      sendEmailCodeApi(email)
        .then((res) => {
          this.$message.success("发送成功，请前往邮箱查看验证码");
          // 开始倒计时
          let countdown = 60;
          this.codeButtonText = `${countdown}秒后重试`;

          this.codeTimer = setInterval(() => {
            countdown--;
            if (countdown <= 0) {
              clearInterval(this.codeTimer);
              this.codeSending = false;
              this.codeButtonText = "发送验证码";
            } else {
              this.codeButtonText = `${countdown}秒后重试`;
            }
          }, 1000);
        })
        .catch((err) => {
          this.$message.error(err.message || "发送失败");
          this.codeSending = false;
        });
    },
    /**
       * 清理定时器
       */
    clearTimer() {
      if (this.codeTimer) {
        clearInterval(this.codeTimer);
      }
      if (this.pollingTimer) {
        clearInterval(this.pollingTimer);
      }
    },

    /**
     * 回到首页
     */
    backToHome() {
      this.$router.push("/");
    },
  },
  beforeDestroy() {
    enableScroll();
    this.clearTimer();
  },
mounted() {
    const urlParams = new URLSearchParams(window.location.search);
    const code = urlParams.get('code');
    const message = urlParams.get('message');
    if(code == 200 && message){
      this.$message.success(message)
    }else if(message){
      this.$message.error(message)
    }
  }
};
</script>
<style scoped lang="scss">
.login-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--background);
  min-height: 100vh;
  z-index: 2000;
  transition: background 0.3s ease;
}

.login-body {
  width: 420px;
  padding: 32px;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border-color);
  position: relative;
  transition: all 0.3s ease;
}

.form-container {
  animation: fadeIn 0.3s ease;
}

.form-item {
  margin-bottom: 20px;

  :deep(.el-input__inner) {
    height: 44px;
    font-size: 14px;

    &::placeholder {
      color: #9ca3af;
    }
  }

  :deep(.el-input__prefix) {
    left: 12px;
    color: var(--text-secondary);
  }
}

.submit-btn {
  width: 100%;
  height: 42px;
  border: none;
  border-radius: 8px;
  background: $primary;
  color: #fff;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    background: darken($primary, 10%);
    transform: translateY(-1px);
  }

  &:active {
    transform: translateY(0);
  }
}

.divider {
  margin: 24px 0;
  color: var(--text-secondary);

  :deep(.el-divider__text) {
    background-color: var(--surface);
    padding: 0 12px;
    font-size: 14px;
  }
}

.third-party-login {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  max-width: 340px;
  margin: 0 auto 24px;
  gap: 16px;
}

.login-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  cursor: pointer;
  transition: all 0.2s;
  background: var(--hover-bg);

  &:hover {
    transform: translateY(-2px);
    background: var(--border-color);
  }

  &.github {
    color: #24292e;
  }

  &.qq {
    color: #12b7f5;
  }

  &.wechat {
    color: #07c160;
  }

  &.gitee {
    color: #c71d23;
  }

  &.weibo {
    color: #e6162d;
  }

  &.alipay {
    color: #1677ff;
  }

  &.baidu {
    color: #317ef3;
  }

  &.huawei {
    color: #c71d23;
  }

  &.xiami {
    color: #ff6700;
  }

  &.weiruan {
    color: #0078d4;
  }

  &.dingding {
    color: #13c2c2;
  }

  &.douyin {
    color: #ff2442;
  }
}

.form-switch {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 24px;
  color: var(--text-secondary);
  font-size: 14px;

  a {
    color: $primary;
    text-decoration: none;
    font-weight: 500;
    cursor: pointer;

    &:hover {
      color: darken($primary, 10%);
    }
  }
}

.divider-line {
  color: var(--border-color);
  margin: 0 12px;
}

.qrcode-content {
  padding: 24px;
  text-align: center;
  animation: fadeIn 0.3s ease;
}

.qrcode-box {
  width: 200px;
  height: 200px;
  margin: 0 auto 16px;
  padding: 8px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  background: var(--surface);

  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

.qrcode-tip {
  margin: 8px 0;
  color: var(--text-secondary);
  font-size: 14px;
}

.code-text {
  color: #6366f1;
  font-weight: 500;

  i {
    cursor: pointer;
    margin-left: $spacing-sm;
  }
}

.form-header {
  text-align: center;
  margin-bottom: 32px;
}

.form-title {
  font-size: 24px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 8px;
}

.form-subtitle {
  font-size: 14px;
  color: var(--text-secondary);
  margin: 0;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.forgot-link {
  color: $primary;
  font-size: 14px;
  cursor: pointer;

  &:hover {
    color: darken($primary, 10%);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

.switch-form-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  background: var(--hover-bg);
  border: none;
  color: var(--text-secondary);

  &:hover {
    background: var(--border-color);
    transform: rotate(180deg);
  }

  i {
    font-size: 20px;
  }
}

.back-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  background: var(--hover-bg);
  border: none;
  color: var(--text-secondary);
  z-index: 1;

  &:hover {
    background: var(--border-color);
    transform: translateX(-4px);
    color: $primary;
  }

  i {
    font-size: 20px;
  }
}
</style>
